package com.zzwtel.autocode.beetl;

import java.util.List;

import com.beetl.ext.fn.StringUtil;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.zzwtel.autocode.template.model.EntityModel;
import com.zzwtel.autocode.template.model.Property;
import com.zzwtel.autocode.template.model.SqlTemplate;

/**
 * sql语句构造器
 * 
 * @author 草原狼
 * @date 2017-7-3
 */
public class SqlBuilderTemplate {
	
	/**
	 * 拼接insert语句
	 * @param tm
	 * @return
	 */
	public static SqlTemplate getInsertSql(EntityModel tm) {
		StringBuffer sb = new StringBuffer();
		StringBuffer sbparam = new StringBuffer();
		List<String> columns = Lists.newArrayList();
		List<String> fields = Lists.newArrayList();
		List<String> params = Lists.newArrayList();
		for (Property prop : tm.getProps()) {
			/*
			if (prop.isKey())
				continue;
			*/
			columns.add(prop.getColumn());
			fields.add("?");
			//fields.add(":" + prop.getName());			
			String method = getMethodName(prop);
			params.add(method);
		}
		sb.append("insert into " + tm.getTable().getName() + " (" + Joiner.on(",").join(columns) + ") values ("
				+ Joiner.on(",").join(fields) + ")");
		sbparam.append("new Object[]{"+ Joiner.on(",").join(params)+"};");
		
		return new SqlTemplate(sb.toString(),sbparam.toString());
	}
	
	private static String getMethodName(Property prop){
		StringUtil stringUtil = new StringUtil();		
		if("Boolean".equals(prop.getJavaType())){
			return "bean.is"+stringUtil.capitalize(prop.getName())+"()";
		}else{
			return "bean.get"+stringUtil.capitalize(prop.getName())+"()";
		}
		
	}
	/**
	 * 拼接update语句
	 * @param tm
	 * @return
	 */
	public static String getUpdateSql(EntityModel tm){
		StringBuffer sb = new StringBuffer();
		StringBuffer sbparam = new StringBuffer();
        List<String> sets= Lists.newArrayList();
        List<String> wheres= Lists.newArrayList();
        List<String> params = Lists.newArrayList();
        for(Property prop : tm.getProps()){
            //wheres.add(prop.getColumn()+"=:"+prop.getName());
            wheres.add(prop.getColumn()+"= ?");
            if(prop.isKey()) continue;
            //sets.add(prop.getColumn()+"=:"+prop.getName());
            sets.add(prop.getColumn()+"= ?");
            String method = getMethodName(prop);
			params.add(method);
        }
		sb.append("update "+tm.getTable().getName()+" set "+Joiner.on(",").join(sets)+" where "+Joiner.on(",").join(wheres));
		//sbparam.append("new Object[]{"+ Joiner.on(",").join(params)+"};");
		//return new SqlTemplate(sb.toString(),sbparam.toString());
		return sb.toString();
	}
	/**
	 * 拼接update语句
	 * @param tm
	 * @return
	 */
	public static SqlTemplate getUpdateByPKSql(EntityModel tm) {
		StringBuffer sbparam = new StringBuffer();
        StringBuffer sb = new StringBuffer();
        List<String> sets= Lists.newArrayList();
        List<String> params = Lists.newArrayList();
        String where="";
        String isKeyMethod = "";
        for(Property prop : tm.getProps()){
            if(prop.isKey()){
                //where+=prop.getColumn()+"=:"+prop.getName();
                where+=prop.getColumn()+"= ?";
                isKeyMethod = getMethodName(prop);
            }else{
                //sets.add(prop.getColumn()+"=:"+prop.getName());
                sets.add(prop.getColumn()+"= ?");
                String method = getMethodName(prop);
    			params.add(method);
            }
        }
        params.add(isKeyMethod);
        sb.append("update "+tm.getTable().getName()+" set "+Joiner.on(",").join(sets)+" where "+where);
        sbparam.append("new Object[]{"+ Joiner.on(",").join(params)+"};");
        return new SqlTemplate(sb.toString(),sbparam.toString());
    }
	
	/**
	 * 拼接select语句
	 * @param tm
	 * @return
	 */
	public static SqlTemplate getSelectSql(EntityModel tm) {
		String tableName = tm.getTable().getName();
		StringBuffer sbparam = new StringBuffer();
        StringBuffer sb = new StringBuffer();
        List<String> sets= Lists.newArrayList();
        List<String> columns= Lists.newArrayList();
        List<String> params = Lists.newArrayList();
        for(Property prop : tm.getProps()){
            columns.add(prop.getColumn());
            //sets.add(prop.getColumn()+"=:"+prop.getName());
            sets.add(prop.getColumn()+"= ?");
            params.add(getMethodName(prop));
        }
        sb.append(String.format("select %s from %s where %s",Joiner.on(",").join(columns),tableName,Joiner.on(" and ").join(sets)));
        sbparam.append("new Object[]{"+ Joiner.on(",").join(params)+"};");
        return new SqlTemplate(sb.toString(),sbparam.toString());
    }
    public static SqlTemplate getDeleteSql(EntityModel tm) {
    	StringBuffer sbparam = new StringBuffer();
    	String tableName = tm.getTable().getName();
        StringBuffer sb = new StringBuffer();
        List<String> sets= Lists.newArrayList();
        List<String> params = Lists.newArrayList();
        String isKeyMethod = "";
        for(Property prop : tm.getProps()){
            //sets.add(prop.getColumn()+"=:"+prop.getName());
        	if(prop.isKey()){
        		isKeyMethod = getMethodName(prop);
        	}else{
        		sets.add(prop.getColumn()+"= ?");
        		String method = getMethodName(prop);
     			params.add(method);
        	}            
        }
        params.add(isKeyMethod);
        sb.append(String.format("delete from %s where %s",tableName,Joiner.on(" and ").join(sets)));
        sbparam.append("new Object[]{"+ Joiner.on(",").join(params)+"};");
        return new SqlTemplate(sb.toString(),sbparam.toString());
    }
}
